from pymongo import MongoClient
from sendEmail import mail
from flask import request
from flask import Flask

import datetime
import socket
import pprint
import time
import json
import sys

machine = socket.gethostname().split('.')[0].split('de')[1]
client = MongoClient("localhost", 27017)
tb_scrapy = client.tb_scrapy
dest_collection = None
app = Flask(__name__)

if len(sys.argv) == 2 and sys.argv[1] == 'True':
    dest_collection = tb_scrapy.boxandneedle
    print('True')
else:
    print('False')
    dest_collection = tb_scrapy.boxandneedle_test

start, count = None, 0

@app.route("/scrapy", methods = ["GET", "POST"])
def scrapy():
    global count, start

    product = json.loads(request.form['product'])

    if (product["title"] == ""):
        print(product["title"], '获取不到数据')
        return '{"success":false}'

    inserted_id = dest_collection.insert_one({
        "time": datetime.datetime.now(),
        "favcount": product["favcount"],
        "shopName": product["shopName"],
        "title": product["title"],
        "data": product["data"]
    }).inserted_id

    if inserted_id != None:
        count += 1
        print('-' * 50, '[', count, ']', '-' * 50)
        pprint.pprint(product)

    if start is None:
        start = time.time()

    return '{"success":true}'

@app.route("/sendEmail", methods = ["GET", 'POST'])
def sendEmail():
    global count, start

    total = dest_collection.count()
    usedMinutes = int((time.time() - start) // 60)
    usedSeconds = round((time.time() - start) % 60, 1)

    info = "[{0}]:早上好!指南针店铺数据抓取正常结束，此次一共抓取{1}个宝贝。总用时:{2}分{3}秒。当前已累计{4}条数据".format(machine, count, usedMinutes, usedSeconds, total)

    print('*' * 150)
    print(info)
    print('*' * 150)

    mail(info)

    count, start = 0, None

    return '{"success":true}'

if __name__ == "__main__":
    try:
        app.run()
    except KeyboardInterrupt:
        print("the scrapy server is end")
